package karaokit.com;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.TreeMap;

public class Lyrics {
	
	/*
	 * An array of the lines of the lyric file
	 */
	public TreeMap<LyricsTime, String> _lyrics;
	int _curPosition = 0;
	int _curTimeFrame;
	public static DateFormat LyricsFormatter = new SimpleDateFormat("mm:ss");
	static String TimePattern1 = "\\d{1,2}:\\d{1,2}";
	static String TimePattern2 = "\\d{1,2}:\\d{1,2}.\\d{3}";
	
	class LyricsTime implements Comparable<LyricsTime> {		
		private int _start;
		public LyricsTime(int start, int end) {
			this._start = start;
			this._end = end;
		}
		public int get_start() {
			return _start;
		}
		public void set_start(int _start) {
			this._start = _start;
		}
		public int get_end() {
			return _end;
		}
		public void set_end(int _end) {
			this._end = _end;
		}
		private int _end;

		//a negative integer, zero, or a positive integer as this 
		//object is less than, equal to, or greater than the specified object. 
		public int compareTo(LyricsTime time) {
			if (this._start >= time.get_start()) {
				if (this._end <= time._end)
					return 0;
				return 1;
			}
			else if (this._end >= time.get_end()) 
				return 0;			
			return -1;
		}	
	}

	/*
	 * Load the selected lyrics file to the application
	 */
	public Lyrics(File lyricFile) {
		_lyrics = new TreeMap<LyricsTime, String>();
		int startTime = 0;
		String lyrics = "";
		try {
			BufferedReader read  = new BufferedReader(new FileReader(lyricFile));
			String line, timeStamp;			

			while ((line = read.readLine()) != null) {				
				if (line.equals("") || !line.contains("[") || !line.contains("]"))
					continue;
				timeStamp = line.substring(line.indexOf("[")+1, line.indexOf("]"));
				if (!timeStamp.matches(TimePattern1) && !timeStamp.matches(TimePattern2))
					continue;				

				int endTime = toMillis(timeStamp); 
				if (endTime != 0) {
					_lyrics.put(new LyricsTime(startTime,endTime), lyrics);
				}
				startTime = endTime; // for the next iteration
				lyrics = (line.endsWith("]")) ?
						"" : line.substring(line.indexOf("]")+1);				
			}
			_lyrics.put(new LyricsTime(startTime, Integer.MAX_VALUE), lyrics);
			read.close();			
		} catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();			
		}
	}


	/**
	 * @param d - the time requested
	 * @return the string that should be shown
	 */
	public String getLine(int d){		
		return _lyrics.get(new LyricsTime(d,d));
	}
	
	public static int toMillis(String time){
		if (time.contains(".")) {
			return (Integer.parseInt(time.substring(0, time.indexOf(":")))*60*1000) +
			(Integer.parseInt(time.substring(time.indexOf(":")+1, time.indexOf(".")))*1000 + 
			(Integer.parseInt(time.substring(time.indexOf(".")+1))));
		}
		return (Integer.parseInt(time.substring(0, time.indexOf(":")))*60*1000) +
			(Integer.parseInt(time.substring(time.indexOf(":")+1)))*1000;
	}
	
	public void printAll() {			
		Iterator<LyricsTime> iter = _lyrics.keySet().iterator();
		while (iter.hasNext()) {
			LyricsTime t = iter.next();
			String lyrics = _lyrics.get(t);
			System.out.println("[" + t.get_start() + "-" + t.get_end() + "] " + lyrics); 		
		}
	}

	public static void main(String[] args)  {
		//Lyrics l = new Lyrics(new File("c:\\Heal the World.lrc"));
		Lyrics l = new Lyrics(new File("c:\\RobertCray.lrc"));
		
		l.printAll();
		/*Lyrics l = new Lyrics(new File("c:\\Heal the World.lrc"));
		while (LyricsTime cur :  l._lyrics.keySet()) {
			
		}
		while (iter.hasNext()) {
			LyricLine line = iter.next();
			System.out.println("["+line+"]");
		}
		
		int d  = toMillis("[02:18.444]");
		System.out.println(l.getLine(d));*/

		/*String datePattern1 = "\\d{1,2}:\\d{1,2}";
		String datePattern2 = "\\d{1,2}:\\d{1,2}.\\d{3}";

		String date;
		date = "1:2";
		System.out.println(date + " -> " + date.matches(datePattern1));
		date = "05:89";
		System.out.println(date + " -> " + date.matches(datePattern1));
		
		date = "1:2.300";
		System.out.println(date + " -> " + date.matches(datePattern2));
		date = "13:24.03";
		System.out.println(date + " -> " + date.matches(datePattern2));
		date = "1:23.111";
		System.out.println(date + " -> " + date.matches(datePattern2));*/
		
	}
}
